package com.cyl.magic.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ssssssss.magicapi.modules.db.model.PageResult;

import com.cyl.external.MyEasyExcelMapUtil;
import com.cyl.magic.query.MagicQuery;
import com.cyl.magic.service.MyMagicService;
import com.cyl.manager.aws.domain.entity.ExportExcel;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.SecurityUtils;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 数据导出管理Controller
 *
 * @author zcc
 * @date 2025-03-07
 */
@Api(description ="统一导出功能")
@RestController
@RequestMapping("/aws/exportExcel")
public class MagicExcelController extends BaseController {
    @Autowired
    private MyMagicService myMagicService;
    
    
    @ApiOperation("查询数据导出管理列表")
    @PostMapping("/page")
    public ResponseEntity<PageResult<LinkedHashMap<String, Object>>> page(@RequestBody MagicQuery magicQuery) {
    	Long deptZbjId = SecurityUtils.getDeptZbjId();
    	Map<String, Object>	params = magicQuery.getParams();
    	params.put("deptZbjId", deptZbjId);
        params.put("page",magicQuery.getPage());
        params.put("size", magicQuery.getSize());
        String url = "/out"+magicQuery.getService();
    	PageResult<LinkedHashMap<String, Object>> page  = myMagicService.page(url, params);
        return ResponseEntity.ok(page);
    }
    

    @ApiOperation("查询数据导出管理列表")
    @PostMapping("/export")
    public ResponseEntity<ExportExcel> export(@RequestBody MagicQuery magicQuery) {
    	Long deptZbjId = SecurityUtils.getDeptZbjId();
    	Map<String, Object>	params = magicQuery.getParams();
    	params.put("deptZbjId", deptZbjId);
        params.put("page",1);
        params.put("size", 1);
        String url = "/out"+magicQuery.getService();
    	PageResult<LinkedHashMap<String, Object>> page  = myMagicService.page(url, params);
    	long total = page.getTotal();
    	if(total == 0L) {
            throw new RuntimeException("没有可以导出的数据");
    	}
    	
    	
       long time = System.currentTimeMillis();
       MyEasyExcelMapUtil outExcelMapUtil = null;
       ExportExcel exportExcel = null;
       try {
    	   exportExcel = new ExportExcel(JSONUtil.toJsonStr(params), magicQuery.getName(), magicQuery.getName(), time, total);
    	   LinkedHashMap<String, Object> map = page.getList().get(0);
          
    	   outExcelMapUtil = new MyEasyExcelMapUtil(map, exportExcel);
    	   int pageSize = 2000;
           int leng = outExcelMapUtil.getLength(total, pageSize);
           for (int i = 0; i < leng; i++) {
    	    	params.put("page",i+1);
    	        params.put("size", pageSize);  
    	      	PageResult<LinkedHashMap<String, Object>> pageWrite  = myMagicService.page(url, params);
    	      	outExcelMapUtil.writeList(pageWrite.getList());
    	   }
           outExcelMapUtil.finish();

		} catch (Exception e) {
			e.printStackTrace();
            throw new RuntimeException(e.getMessage());
		}finally {
			if(outExcelMapUtil!=null){
				outExcelMapUtil.finallyClose();
		   } 
		}
       if(outExcelMapUtil!=null) {
           return ResponseEntity.ok(outExcelMapUtil.getExportExcel());
       }
       return ResponseEntity.ok(null);
      
          
    	
    }
 
}
